<template>
  <div class="articleIndex">
    <van-sticky>
      <div class="navTitle">
        <div class="recommend" @click="selectData">推荐</div>
        <div class="Latest" @click="Latest">最新</div>
        <div class="right-Img"></div>
      </div>
    </van-sticky>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-divider />
      <div class="item" v-for="item in faceList" :key="item.id" @click="$router.push({ path: '/detail', query: { id: item.id } })">
        <div class="top">
          <!-- <div class="left">1</div> -->
          <img :src="item.avatar" alt="" />
          <div class="right">
            <div class="title">{{ item.stem }}</div>
            <div class="username">
              <span>{{ item.creator }}</span>
              <span>|</span>
              <span>{{ item.createdAt }}</span>
            </div>
          </div>
        </div>
        <div class="middle">
          {{ item.content }}
        </div>
        <div class="bottom">
          <span>点赞 {{ item.likeCount }}</span>
          <span>|</span>
          <span>浏览 {{ item.views }}</span>
        </div>
        <van-divider />
      </div>
    </van-pull-refresh>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'Index-Article',
  data() {
    return {
      isLoading: false,
      current: 1, //当前页码
      pageSize: 10, //每页条数
      stem: '', //标题
      keyWord: '', //关键词
      sorter: '',
      faceList: [],
    }
  },
  created() {
    this.selectData()
  },
  methods: {
    //推荐
    async selectData() {
      const res = await axios({
        url: 'http://interview-api-t.itheima.net/h5/interview/query',
        params: {
          current: this.current,
          pageSize: this.pageSize,
          sorter: 'weight_desc',
        },
        headers: {
          Authorization: `Bearer ${localStorage.getItem('token')}`,
        },
      })
      console.log(res)
      if (res.status === 200) {
        this.faceList = res.data.data.rows
      }
    },
    //最新
    async Latest() {
      const res = await axios({
        url: 'http://interview-api-t.itheima.net/h5/interview/query',
        params: {
          current: this.current,
          pageSize: this.pageSize,
        },
        headers: {
          Authorization: `Bearer ${localStorage.getItem('token')}`,
        },
      })
      console.log(res)
      if (res.status === 200) {
        this.faceList = res.data.data.rows
      }
    },
    onRefresh() {
      setTimeout(() => {
        this.selectData()
        this.$toast('刷新成功')
        this.isLoading = false
      }, 1000)
    },
  },
}
</script>

<style lang="less" scoped>
.navTitle {
  display: flex;
  height: 40px;
  background-color: #fff;
  // background-color: #e43c2f;
  // justify-content: space-around;
  justify-content: end;
  align-items: center;
  padding: 0 10px;
  color: #ccc;
  .Latest {
    flex: 1;
    margin-left: 20px;
  }
  .right-Img {
    flex: 1;
    width: 70px;
    height: 30px;
    background-image: url(@/assets/right.png);
    background-size: contain;
    background-repeat: no-repeat;
    margin-left: 180px;
  }
}
.item {
  flex: 1;
  font-size: 15px;
  padding: 10px;
  margin: 10px;
  .top {
    display: flex;

    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }

    .right {
      flex: 1;
    }
  }
}
// 超过两行显示省略号
.middle {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2; /* 添加标准属性 */
  -webkit-box-orient: vertical;
  margin-top: 10px;
  margin-bottom: 10px;
}
</style>
